<template>
	<view>

		<view class="opinion">
			<editor			
						class="myEdit"
						placeholder="写点什么吧~~"
						show-img-size
						show-img-toolbar
						show-img-resize
						@ready="onEditReady"
						@focus="onFocus"
						@statuschange="onStatuschange"
						></editor>
		</view>

		<u-button type="primary" class="button">发表</u-button>


		<view class="tools" v-if="toolShow">
			<view class="item" @click="clickHead">
				<text :class="headShow ? 'active' : ''">H</text>
			</view>
			<view class="item" @click="clickBold">
				<text :class="boldShow ? 'active': ''">B</text>
			</view>
			<view class="item" @click="clickItalic">
				<text :class="italicShow ? 'active' : ''">/</text>
			</view>
			<view class="item" @click="clickDivider">
				<text :class="italicShow ? 'active' : ''">_</text>
			</view>
			<view class="item" @click="clickInsertImage">
				<u-icon name="photo" size="28"></u-icon>
			</view>
			<view class="item" @click="editOk">
				<u-icon name="checkbox-mark" size="28"></u-icon>
			</view>
		</view>
	</view>
</template>

<script>
	import {getImgSrc,getProvince} from "@/utils/tools.js"	
	const db=uniCloud.database()
	export default {
		data() {
			return {
				toolShow: false,
				headShow: false,
				boldShow: false,
				italicShow: false,
				artObj: {
					title: "",
					content: "",
					description: "",
					picurls: "",
					province: ""
				}
			};
		},
		onLoad() {
			//获取地址信息
			// getProvince().then(res => {
			// 	this.artObj.province = res
			// })
		},

		methods: {
			//点击提交按钮
			onSubmit() {
				this.editorCtx.getContents({
					success: res => {
						this.artObj.description = res.text.slice(0, 80);
						this.artObj.content = res.html;
						this.artObj.picurls = getImgSrc(res.html)
						uni.showLoading({
							title: "发布中..."
						})
						this.addData();
					}
				})
			},

			addData() {
				db.collection("quanzi_article").add({
					...this.artObj
				}).then(res => {
					uni.hideLoading();
					uni.showToast({
						title: "发布成功"
					})
					setTimeout(() => {
						uni.reLaunch({
							url: "/pages/index/index"
						})
					}, 800)
				})
			},



			//初始化
			onEditReady() {
				uni.createSelectorQuery().in(this).select('.myEdit').fields({
					size: true,
					context: true
				}, res => {
					this.editorCtx = res.context
				}).exec()
			},


			checkStatus(name, detail, obj) {
				if (detail.hasOwnProperty(name)) {
					this[obj] = true;
				} else {
					this[obj] = false;
				}
			},


			// 当样式发生改变的时候
			onStatuschange(e) {
				console.log(e);
				let detail = e.detail 
				this.checkStatus("header", detail, "headShow");
				this.checkStatus("bold", detail, "boldShow");
				this.checkStatus("italic", detail, "italicShow");
			},


			//添加图像
			clickInsertImage() {
				uni.chooseImage({
					success: async res => {
                          console.log(res);
						uni.showLoading({
							title: "上传中请稍后",
							mask: true
						})
						for (let item of res.tempFiles) {
							let suffix = item.path.substring(item.path.lastIndexOf("."));
							let randomName = Date.now() + "" + String(Math.random()).substr(3, 6) + suffix

							let res = await uniCloud.uploadFile({
								filePath: item.path,
								cloudPath: item.name || randomName
							})
							this.editorCtx.insertImage({
								src: res.fileID
							})
						}
						uni.hideLoading()
					}
				})
			},


			//点击工具条的确认
			editOk() {
				this.toolShow = false;
			},

			//加粗
			clickBold() {
				this.boldShow = !this.boldShow
				this.editorCtx.format("bold")
			},

			//设置倾斜
			clickItalic() {
				this.italicShow = !this.italicShow;
				this.editorCtx.format("italic")
			},

			//添加大标题
			clickHead() {
				this.headShow = !this.headShow
				this.editorCtx.format("header", this.headShow ? 'H2' : false)
			},

			//添加分割线
			clickDivider() {
				this.editorCtx.insertDivider();
			},


			//离开焦点
			onFocus() {
				this.toolShow = true
			}
		}
	}
</script>

<style lang="scss">
	.opinion{
		width: 90%;
		padding: 20rpx 30rpx;
		.myEdit{
			height: calc(100vh - 500rpx);
						margin-bottom:30rpx;
		}
	}
	.button {
		width: 100%;
		position: fixed;
		bottom: 100rpx;
	}

	.tools {
		width: 100%;
		height: 80rpx;
		background: #fff;
		border-top: 1rpx solid #f4f4f4;
		position: fixed;
		left: 0;
		bottom: 0;
		display: flex;
		justify-content: space-around;
		align-items: center;
		.item{
		 .active{
		 	color: blue;
		}
		}
	}
</style>